package com.ogawa.chair7808.ui.activity;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.StringUtils;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.bean.Massage7808Armchair;
import com.ogawa.chair7808.manager.DataManager;
import com.example.reslib.utils.ToastUtils;
import com.ogawa.base.Constant.CommmandNum;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 肩部调整
 */
public class ShouldersAdjustmentActivity7808 extends BaseMqtt7808Activity implements View.OnClickListener
//        ,IShoulderAdjustView7506 ,CloseScanAndAdjust.onCloseActivity
{


    private ImageView mIvUp;
    private ImageView mIvDown;
    private int count = 1;
    private int mPoint;//机芯位置
    //    private ShoulderAdjustPresenter7506 mPresenter;
    private boolean isClick = false;
    private long mClickTime = 0;
    private String mAction = "";//按上up   按下dowm
    private Disposable mDisposable;

    @Override
    public int setLayoutId() {
        return R.layout.activity_shoulders_adjustment_7808;
    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        initView();
    }


    @Override
    public void initTitleBar() {
        super.initTitleBar();
        showTitleBar(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mPresenter.onDestoty();
//        CloseScanAndAdjust.getInst().removeObserver(this);
    }


    private void initView() {
//        mPresenter = new ShoulderAdjustPresenter7506(this);
//        mPoint =Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusPointPosition)) ;
//        Log.e("getPoint_init",mPoint+"");
        mIvUp = findViewById(R.id.img_shoulder_up);
        mIvDown = findViewById(R.id.img_shoulder_down);
        findViewById(R.id.img_shoulder_ok).setOnClickListener(this);
        mIvUp.setOnClickListener(this);
        mIvDown.setOnClickListener(this);
//        CloseScanAndAdjust.getInst().registerObserver(this);
    }

    private void setClick() {
        isClick = true;
        mClickTime = System.currentTimeMillis();
        mIvUp.setEnabled(false);
        mIvDown.setEnabled(false);
        clickButton();
    }

    public void showUpDown() {
        isClick = false;
        mIvUp.setEnabled(true);
        mIvDown.setEnabled(true);
        if (mDisposable != null) {
            mDisposable.dispose();
        }

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.img_shoulder_ok) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getInst().sendCommond(CommmandNum.JIANBQR, "1");
                this.finish();
            }

        } else if (i == R.id.img_shoulder_up) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                Log.e("getPoint_up", mPoint + "");
                if (mPoint < 10) {
                    mAction = "up";
                    setClick();
                    DataManager.getInst().sendCommond(CommmandNum.JIXINUP, "1");
                } else {
                    ToastUtils.showShortToast(this, "机芯已在最高处");
                }

            }

        } else if (i == R.id.img_shoulder_down) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                Log.e("getPoint_down", mPoint + "");
                if (mPoint > 0) {
                    mAction = "down";
                    setClick();
                    DataManager.getInst().sendCommond(CommmandNum.JIXINDOWM, "2");
                } else {
                    ToastUtils.showShortToast(this, "机芯已在最低处");
                }

            }

        }
    }


    public void clickButton() {
        Observable.interval(0, 300, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {
                        if (isClick && System.currentTimeMillis() - mClickTime > 3000) {
                            showUpDown();

                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        showUpDown();

                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

//    @Override
//    public void isFinish() {
//        this.finish();
//
//    }
//
//    @Override
//    public void getPoint(int position) {
//        //如果是点击了上
//        if(!TextUtils.isEmpty(mAction)&&mAction.equals("up")){
//            if(position>mPoint){
//                showUpDown();
//                mAction ="";
//            }
//        }
//       else if(!TextUtils.isEmpty(mAction)&&mAction.equals("down")){
//            if(position<mPoint){
//                showUpDown();
//                mAction ="";
//            }
//        }
//        mPoint = position;
//
//
//    }
//
//    @Override
//    public void onClose() {
//        this.finish();
//    }


    @Override
    public void changeViewByMqttMessage() {
        String modelValue = "0";
        modelValue = DataManager.getInst().getDevice7808StateBean().getShoulderDetectCheck();
        if ("0".equals(modelValue) || "4".equals(modelValue)) {
            finish();
        }
        String yPos = DataManager.getInst().getDevice7808StateBean().getYPos();
        mPoint = StringUtils.getValue(yPos);

    }
}
